package com.android.shop.ui;

import java.util.ArrayList;
import java.util.List;
import com.android.shop.R;
import com.android.shop.api.MyHttpClient;
import com.android.shop.bean.Dish;
import com.android.shop.bean.DishCart;
import com.android.shop.constant.MyConstantID;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AddFoodActivity extends BaseActivity {

	private EditText foodName;
	private EditText price;
	private Button submit;
	private TextView hdcartid;
	private RadioGroup group;
	private RadioButton shangjia,xiajia;
	String _state="1";
	private Spinner spinner;
	List<DishCart> lst = new ArrayList<DishCart>();
	private ArrayAdapter<DishCart> adapter;
	String result;
	String type="add";
	String dishid;
	Dish dishbean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_food_layout);
		new Thread(new ReceiveThread()).start();
		
		foodName = (EditText) this.findViewById(R.id.foodNameEdit);
		price = (EditText) this.findViewById(R.id.price_Edit);
		hdcartid = (TextView) this.findViewById(R.id.hdcartid);
		group = (RadioGroup) findViewById(R.id.rgState);
		submit = (Button) findViewById(R.id.submit_food);
		shangjia=(RadioButton)findViewById(R.id.rb_shangjia);
		xiajia=(RadioButton)findViewById(R.id.rb_xiajia);
		

		
		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.rb_shangjia) {
					_state="1";
				} else if (checkedId == R.id.rb_xiajia){
					_state="0";
				}
			}
		});
		
		submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new Thread(new SubmitThread()).start(); 
			}
		});
	}

	class ReceiveThread implements Runnable {

		@Override
		public void run() {
			SharedPreferences sp = getSharedPreferences(MyConstantID.SP_NAME,
					Activity.MODE_PRIVATE);
			String sellerid = sp.getString("sellerid", "");
			lst = MyHttpClient.getSingle().getCatsBySeller(sellerid);
			handler.sendEmptyMessage(11);
		}
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 11) {
				if (lst == null) {
					Toast.makeText(AddFoodActivity.this, "服务器繁忙，请稍后再试",
							Toast.LENGTH_LONG).show();
				} else {
					spinner = (Spinner) findViewById(R.id.cart_choose);
					adapter = new ArrayAdapter<DishCart>(getBaseContext(),
							android.R.layout.simple_spinner_item, lst);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					spinner.setAdapter(adapter);
					spinner.setPrompt("请选择类别");
					spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
					spinner.setVisibility(View.VISIBLE);
					
					//修改
					Bundle bundle=getIntent().getExtras();
					if(bundle!=null){
						dishid=bundle.getString("dishid");
						if(dishid!=null&&!dishid.equals("")){
							type="update";
							new Thread(new RecThread()).start();
						}
					}
				}
			}
		}
	};

	class SpinnerSelectedListener implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			String cartid = ((DishCart) spinner.getSelectedItem()).getId();
			hdcartid = (TextView) findViewById(R.id.hdcartid);
			if (cartid != null)
				hdcartid.setText(cartid);
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			Toast.makeText(getApplicationContext(), "请选择类别", Toast.LENGTH_SHORT)
					.show();
		}
	}

	public void submitDataToServer() {
		
		String _name = foodName.getText().toString();
		String _price = price.getText().toString();
		String _cartid = hdcartid.getText().toString();
		String _dishphoto = "";

		if (_name.equals("")) {
			Toast.makeText(getApplicationContext(), "名称不能为空",
					Toast.LENGTH_SHORT).show();
		}
		if (_price.equals("")) {
			Toast.makeText(getApplicationContext(), "价格不能为空",
					Toast.LENGTH_SHORT).show();
		}
		if (_cartid.equals("")) {
			Toast.makeText(getApplicationContext(), "请选择菜目", Toast.LENGTH_SHORT)
					.show();
		}
		SharedPreferences sp = getSharedPreferences(MyConstantID.SP_NAME,
				Activity.MODE_PRIVATE);
		String sellerid = sp.getString("sellerid", "");
		result = MyHttpClient.getSingle().addDish(_name, _price, _cartid,
				_dishphoto,_state, sellerid,dishid,type);
	}

	class RecThread implements Runnable {
		@Override
		public void run() {
			dishbean= MyHttpClient.getSingle().getDishById(dishid);
			handlerRec.sendEmptyMessage(1);
		}
	}

	Handler handlerRec = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1) {
				if (dishbean!=null) {
					foodName.setText(dishbean.getDishname());
					price.setText(dishbean.getPrice());
					hdcartid.setText(dishbean.getCartid());
					String catid= dishbean.getCartid();
					int pos=1;
					if(lst!=null&&spinner!=null){
						for(int i=0;i<lst.size();i++){
							if(lst.get(i).getId().equals(catid)){
								pos=i;break;
							}
						}
						spinner.setSelection(pos);
					}				
					
					if(dishbean.getState().equals("0")){
						xiajia.setChecked(true);
					}else if(dishbean.getState().equals("1")){
						shangjia.setChecked(true);
					}
				} else {
					Toast.makeText(getApplicationContext(), "网络异常",
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	};
	
	class SubmitThread implements Runnable {
		@Override
		public void run() {
			submitDataToServer();
			handlerSubmit.sendEmptyMessage(11);
		}
	}

	Handler handlerSubmit = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 11) {
				if (result.equals("SUC")) {
					Toast.makeText(getApplicationContext(), "处理成功",Toast.LENGTH_SHORT).show();
					foodName.setText("");
					price.setText("");
					hdcartid.setText("");
				} else {
					Toast.makeText(getApplicationContext(), "网络异常",
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	};
	
	public void back(View v){
		this.finish();
	}

	
}
